package com.xerland.tbootstrap.core;

import android.support.annotation.NonNull;

import com.xerland.tbootstrap.core.rest.RestApi;
import com.xerland.tbootstrap.core.rest.models.Emojis;
import com.xerland.tbootstrap.core.storage.DbHelper;
import com.xerland.tbootstrap.core.storage.entities.EmojitEntity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Singleton;

import rx.Observable;

@Singleton
public class DataManager {

    private DbHelper dbHelper;
    private RestApi restApi;

    @Inject
    public DataManager(@NonNull DbHelper dbHelper, @NonNull RestApi restApi) {
        this.dbHelper = dbHelper;
        this.restApi = restApi;
    }

//    @NonNull
//    public Observable<Quote> loadQuotes() {
//        return restApi.loadQuotes(ConstantsManager.SITE_PARAM, ConstantsManager.NAME_PARAM,
//                100).concatMap(quotes -> dbHelper.insertQuotes(quotes));
//    }

    @NonNull
    public Observable<Emojis> loadEmojis() {
        return restApi.loadQuotes().flatMap(quotes->{
            List<Emojis> emojisList= new ArrayList<Emojis>();
            for(String key : quotes.keySet()){
                Emojis emojis = new Emojis();
                emojis.setName(key);
                emojis.setUrl(quotes.get(key));
                emojisList.add(emojis);
            }
            return Observable.just(emojisList);
        }).concatMap(quotes -> dbHelper.insertQuotes(quotes));
    }

    @NonNull
    public Observable<List<EmojitEntity>> getQuotes() {
        return dbHelper.selectQuotes();
    }
}